// vuex的状态机,根据功能模块划分为设备管理，设备点检，设备维修，设备保养等
import {
    fetchRoleGroupList,
    saveApproveTemplate,
    delApproveTemplate
} from "@/api/system/index";
import { reduceNullContent } from "@/utils/index";
import { Message } from 'element-ui'
const state = {
    approvePoints: "",
    records: [], //表格数据
    listSearchParams: {},
    pages: 1,
    current: 1,
    total: 0,
    currentApproveId: "",
    approveItem: {
        approves: []
    },
    formSubmit: {}, //审批管理的表单数据
    mesg: "",
    //-----------表单各字段内容----------//
    approveOrgIds: [], //审批范围
    billType: "",
    //审批范围

    /**
     * 编辑相关
     */
    id: ""
};
const mutations = {
    SET_LISTSERACH_PARAMS: (state, args) => {
        state.listSearchParams = args;
    },
    SET_LISTDATA: (state, args) => {
        state.records = args.records;
        state.total = args.total;
        state.current = args.current;
        state.pages = args.pages;
    },
    ADD_LISTDATA: (state, args) => {
        state.records.push(args);
        console.log(state, "++++state+++");
    },
    UPDATE_CONTENT: (state, args) => {
        const data = Object.keys(args)[0]
            // if (args[Object.keys(args)[0]]) {
            //     state.formSubmit[Object.keys(args)[0]] = args[Object.keys(args)[0]]; //动态赋值
            // } else {
            //     delete state.formSubmit[Object.keys(args)[0]] //清除为null的
            // }
        state.formSubmit[Object.keys(args)[0]] = args[Object.keys(args)[0]];
    },
    SET_APPROVEORGIDS: (state, args) => {
        state.approveOrgIds = args; //动态赋值
    },
    SET_BILLTYPE: (state, args) => {
        state.billType = args;
    },
    SET_APPROVEITEM: (state, args) => {
        state.approveItem = args; //视图层的数据
    },
    CLEAR_FORMSUBMIT: (state, args) => {
        state.formSubmit = {}
    }
};

const actions = {
    clearFormSubmit({ commit, state }, args) {
        commit("CLEAR_FORMSUBMIT");
    },
    //增加审批节点
    setPoints({ commit, state }, args) {
        state.approvePoints = args
    },
    // 查询参数
    setListSearchParams({ commit, state }, args) {
        //;
        const data = Object.assign({}, state.listSearchParams, args);
        commit("SET_LISTSERACH_PARAMS", data);
    },

    updateApproveItem({ commit, state }, args) {
        if (args.approves) {
            state.approveItem = args.approves.map((e, i) => {
                e.name = e.resIds;
                e.i = i;
            });

            commit("SET_APPROVEITEM", args);
        } else {
            //空白项则不填充
            commit("SET_APPROVEITEM", {});
        }
    },
    //审批列表
    requestRoleList({ commit, state }, args) {
        return fetchRoleGroupList(reduceNullContent(args)).then(response => {
            console.log("系统权限", response);
            commit("SET_LISTDATA", response.data);
        });
    },
    //新增一条审批模板
    addOneRole({ commit, state }, args) {
        commit("ADD_LISTDATA", args);
    },
    //当审批管理的下拉选框要动态把表单数据记录
    updateSubmitContent({ commit, state }, args) {
        commit("UPDATE_CONTENT", args);
    },

    //根据id删除审批模板
    delApproveTemplate({ commit, state }, args) {
        return delApproveTemplate(args).then(response => {
            console.log("delete:", response);
        });
    },
    //保存或新增审批管理模板  billType organIds  id
    saveTemplate({ commit, state }, args) {
        //把状态数据组装成接口所需数据
        const data = {};
        data.billType = state.billType != '' ? state.billType : state.approveItem.billType;

        data.organIds = state.approveOrgIds.length != 0 ? state.approveOrgIds : state.approveItem.organIds;;
        //data.approves.resMan = state.formSubmit
        const resMan = [
            state.formSubmit.resIds0 ? {
                resMan: state.formSubmit.resIds0.map(e => {
                    return { id: e.split(",")[0], name: e.split(",")[1] };
                })
            } :
            "",
            state.formSubmit.resIds1 ? {
                resMan: state.formSubmit.resIds1.map(e => {
                    return { id: e.split(",")[0], name: e.split(",")[1] };
                })
            } :
            "",
            state.formSubmit.resIds2 ? {
                resMan: state.formSubmit.resIds2.map(e => {
                    return { id: e.split(",")[0], name: e.split(",")[1] };
                })
            } :
            "",
            state.formSubmit.resIds3 ? {
                resMan: state.formSubmit.resIds3.map(e => {
                    return { id: e.split(",")[0], name: e.split(",")[1] };
                })
            } :
            "",
            state.formSubmit.resIds4 ? {
                resMan: state.formSubmit.resIds4.map(e => {
                    return { id: e.split(",")[0], name: e.split(",")[1] };
                })
            } :
            ""
        ];
        const a = [];
        resMan.map(e => {
            if (e != "") {
                a.push(e);
            }
        });
        //data.approveMan = a
        data.approves = a;
        if (state.formSubmit.preserveMan) {
            const preserveMan = [];
            state.formSubmit.preserveMan.map(e => {
                preserveMan.push({ id: e.split(",")[0], name: e.split(",")[1] });
            });
            data.preserveMan = preserveMan;
        }
        if (state.formSubmit.approveMan) {
            const approveMan = [];
            state.formSubmit.approveMan.map(e => {
                approveMan.push({ id: e.split(",")[0], name: e.split(",")[1] });
            });
            data.approveMan = approveMan;
        }

        if (state.formSubmit.checkMan) {
            const checkMan = [];
            state.formSubmit.checkMan.map(e => {
                checkMan.push({ id: e.split(",")[0], name: e.split(",")[1] });
            });
            data.checkMan = checkMan;
        }
        if (state.formSubmit.disMan) {
            const disMan = [];
            state.formSubmit.disMan.map(e => {
                disMan.push({ id: e.split(",")[0], name: e.split(",")[1] });
            });
            data.disMan = disMan;
        }
        if (args.id) {
            data.id = args.id
        }
        return saveApproveTemplate(data)
            // .then(response => {

        //     Message({
        //         message: "处理成功",
        //         type: 'success',
        //         duration: 2 * 1000
        //     })
        // });
    },

    //下拉审批范围变化
    updateApproveOrgIds({ commit, state }, args) {
        commit("SET_APPROVEORGIDS", args);
    },
    updateBillType({ commit, state }, args) {
        commit("SET_BILLTYPE", args);
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};